//
//  TransicationDetailViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/27.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "TransicationDetailViewController.h"
#import "TransicationDetailCell.h"

@interface TransicationDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 currentPage
 */
@property (nonatomic , assign) int currentPage;
/**
 导航明细Btn
 */
@property (nonatomic , strong) UIButton *detailTypeBtn;
/**
 交易类型
 */
@property (nonatomic , strong) NSMutableArray *transicationTypeArr;
/**
 筛选类型
 */
@property (nonatomic , assign) TransicationType transicationType;


@end

@implementation TransicationDetailViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNav];
    [self setUpView];

}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[PullDownMenu sharedInstance] hide];
}


#pragma mark - init method

- (void)setUpNav {

    self.detailTypeBtn.mj_y = self.baseNavView.navTitleLabel.mj_y;
    self.detailTypeBtn.mj_x = (KScreenWidth - self.detailTypeBtn.width)/2.0;
    self.detailTypeBtn.height = self.baseNavView.navTitleLabel.height;
    [self.baseNavView addSubview:self.detailTypeBtn];
    
}


- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    [self.view addSubview:self.myTableView];
}


- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.baseNavView.bottom);
    }];
}


- (void) initAction {
    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArr removeAllObjects];
        self.currentPage = 1;
        [self dataRequest];
    }];
    
    [self.myTableView.mj_header beginRefreshing];
    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.currentPage ++;
        [self dataRequest];
    }];
    
    self.myTableView.mj_footer.hidden = YES;
}


- (void)initData {

    NSArray *iconArr = @[@"user_myasset_transicationdetail_all",
                         @"user_myasset_money",
                         @"user_myasset_transicationdetail_topup",
                         @"user_service_myadopted",
                         @"user_myasset_buymeat",
                         @"user_myasset_transicationdetail_withdrow",
                         @"user_myasset_rewards"];
    NSArray *titleArr = @[@"全部",@"预售款",@"充值",@"养猪",@"买肉",@"提现",@"奖金"];
    
    for (int i = 0; i < 7; i ++) {
        PullDownMenuModel *model = [[PullDownMenuModel alloc] init];
        model.icon = iconArr[i];
        model.title = titleArr[i];
        
        model.isSelect = NO;
        [self.transicationTypeArr addObject:model];
    };
}


#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TransicationDetailCell *cell = [TransicationDetailCell cellWithTableView:tableView];
    if (self.dataArr.count > indexPath.row) {
        cell.model = self.dataArr[indexPath.row];
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


#pragma mark - target action
- (void)detailTypeClick:(UIButton *)button {
    button.selected = !button.selected;

    if (button.selected) {
        @weakify(self);
        [[PullDownMenu sharedInstance] showWithFrame:CGRectMake(0, TopBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT) type:PullDownTypeTransicationTypes dataArr:self.transicationTypeArr completionBlock:^(NSString *text, NSInteger idx ,PullDownType pullWownType) {
            @strongify(self);
            if ([text isEqualToString:@"全部"]) {
                self.transicationType = TransicationTypeAll;
            } else if ([text isEqualToString:@"充值"]){
                self.transicationType = TransicationTypeTopUp;
            } else if ([text isEqualToString:@"养猪"]){
                self.transicationType = TransicationTypeAdoptPig;
            } else if ([text isEqualToString:@"预售款"]){
                self.transicationType = TransicationTypePresell;
            } else if ([text isEqualToString:@"奖金"]){
                self.transicationType = TransicationTypeReward;
            } else if ([text isEqualToString:@"提现"]){
                self.transicationType = TransicationTypeWithdraw;
            } else if ([text isEqualToString:@"买肉"]) {
                self.transicationType = TransicationTypeBugMeat;
            }
            //修改title和img的edge
            [self.detailTypeBtn setTitle:text forState:UIControlStateNormal];
            [self changeBtnEdge];
            self.detailTypeBtn.selected = !self.detailTypeBtn.selected;
            [self.dataArr removeAllObjects];
            self.currentPage = 1;
            [self dataRequest];
        }];
    } else {
        [[PullDownMenu sharedInstance] hide];
    }
}


#pragma mark - private method
- (void)dataRequest {
    TransicationDetailParam *param = [[TransicationDetailParam alloc] init];
    param.memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    param.type = @(self.transicationType);
    param.page = self.currentPage;
    param.limit = 15;
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] tracnsicationDetailWithParam:param] subscribeNext:^(TransicationDetailList * _Nullable transicationDetailList) {
        @strongify(self);
        
        [self.dataArr addObjectsFromArray:transicationDetailList.list];
        
        [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        if (self.currentPage == [transicationDetailList.totalPage intValue] || [transicationDetailList.totalPage intValue] == 0) {
            [self.myTableView.mj_footer endRefreshingWithNoMoreData];;
        } else {
            self.myTableView.mj_footer.hidden = NO;
        }
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    }];
}


//修改明细btn的title和img的edge
- (void)changeBtnEdge {
    NSInteger titleW = self.detailTypeBtn.titleLabel.intrinsicContentSize.width + 2;
    NSInteger imgW = self.detailTypeBtn.imageView.intrinsicContentSize.width + 2;
    [self.detailTypeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -imgW, 0, imgW)];
    [self.detailTypeBtn setImageEdgeInsets:UIEdgeInsetsMake(0, titleW, 0, -titleW)];
}


#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = 75;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}


- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}


- (NSMutableArray *)transicationTypeArr {
    if (!_transicationTypeArr) {
        _transicationTypeArr = [[NSMutableArray alloc] init];
    }
    return _transicationTypeArr;
}


- (UIButton *)detailTypeBtn {
    if (!_detailTypeBtn) {
        _detailTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _detailTypeBtn.frame = CGRectMake(0, 0, 100, 0);
        [_detailTypeBtn setTitle:@"明细" forState:UIControlStateNormal];
        [_detailTypeBtn setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        _detailTypeBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_detailTypeBtn setImage:[UIImage imageNamed:@"switch_black_up"] forState:UIControlStateNormal];
        [_detailTypeBtn setImage:[UIImage imageNamed:@"switch_black_down"] forState:UIControlStateSelected];
        [self changeBtnEdge];
        [_detailTypeBtn addTarget:self action:@selector(detailTypeClick:) forControlEvents:UIControlEventTouchUpInside];
     }
    return _detailTypeBtn;
}


@end
